package cn.mythread.a09threadsafe1;

public class MyThread extends Thread{
    //表示这个类所有的对象，都共享ticket数据
    static int ticket = 0;
    @Override
    public void run() {
        while (true){
            synchronized (MyThread.class) {
                //同步代码块
                if (ticket < 100) {
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket++;
                    System.out.println(getName() + "卖票，票号为：" + ticket);
                } else {
                    break;
                }
            }
        }
    }
}
